<template>
  <div class="app-container">
    <el-table
      v-loading="listLoading"
      :data="projects"
      element-loading-text="加载中"
      border
      fit
      heightlight-current-row
    >
      <el-table-column align="center" width="50" label="序号">
        <template slot-scope="scope">{{ scope.$index + 1 }}</template>
      </el-table-column>

      <el-table-column align="center" width="150" label="项目名称">
        <template slot-scope="scope">
          <a :href="scope.row.url" target="_blank">{{ scope.row.name }}</a>
        </template>
      </el-table-column>

      <el-table-column align="center" label="项目描述">
        <template slot-scope="scope">{{
          scope.row.description.toString()
        }}</template>
      </el-table-column>

      <el-table-column align="center" width="150" label="预览图">
        <template slot-scope="scope">
          <el-image
            :src="scope.row.thumb2"
            style="width: 120px"
            :preview-src-list="srcList"
          ></el-image>
        </template>
      </el-table-column>

      <el-table-column label="操作" width="140" align="center">
        <template slot-scope="scope">
          <el-tooltip
            class="item"
            effect="dark"
            content="github"
            placement="top"
            :hide-after="2000"
          >
            <el-button
              @click="handleOpenGithub(scope.row)"
              type="info"
              icon="iconfont icon-github"
              circle
              size="mini"
            ></el-button>
          </el-tooltip>
          <el-tooltip
            class="item"
            effect="dark"
            content="编辑"
            placement="top"
            :hide-after="2000"
          >
            <el-button
              @click="handleEditProject(scope.row)"
              type="primary"
              icon="el-icon-edit"
              circle
              size="mini"
            ></el-button>
          </el-tooltip>
          <el-tooltip
            class="item"
            effect="dark"
            content="删除"
            placement="top"
            :hide-after="2000"
          >
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              size="mini"
              @click="handleDeleteProject(scope.row)"
            ></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      title="编辑项目信息"
      :visible.sync="dialogFormVisible"
      fullscreen
    >
      <el-form :model="form">
        <el-form-item label="项目名称">
          <el-input v-model="form.name"></el-input>
        </el-form-item>

        <el-form-item label="项目描述（每一项描述以英文逗号分隔）">
          <el-input type="textarea" :rows="5" v-model="form.description"></el-input>
        </el-form-item>

        <el-form-item label="项目链接">
          <el-input v-model="form.url"></el-input>
        </el-form-item>

        <el-form-item label="github地址">
          <el-input v-model="form.github"></el-input>
        </el-form-item>

        <el-form-item label="预览图">
          <Upload :src="form.thumb" @upload-success="handleUploadSuccess" />
        </el-form-item>

        <el-form-item label="排序等级">
          <el-select v-model="form.order" placeholder="分类等级">
            <el-option label="1" value="1"></el-option>
            <el-option label="2" value="2"></el-option>
            <el-option label="3" value="3"></el-option>
            <el-option label="4" value="4"></el-option>
            <el-option label="5" value="5"></el-option>
            <el-option label="6" value="6"></el-option>
          </el-select>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="handleConfirmDialog">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getProject, setProject, delProject } from "@/api/project.js";
import { SERVER_URL } from "@/urlConfig.js";
import Upload from "@/components/Upload";

export default {
  data() {
    return {
      projects: [],
      listLoading: false,
      srcList: [],
      dialogFormVisible: false,
      form: {
        name: "",
        description: "",
        url: "",
        github: "",
        thumb: "",
        order: 1,
      },
    };
  },
  components: {
    Upload,
  },
  created() {
    this.fetchData();
  },
  methods: {
    async fetchData() {
      this.listLoading = true;
      var resp = await getProject();
      this.projects = resp.data.map((item) => {
        var newItem = {
          ...item,
          thumb2: SERVER_URL + item.thumb,
        };
        this.srcList.push(newItem.thumb2);
        return newItem;
      });
      this.listLoading = false;
    },
    handleOpenGithub(projectInfo) {
      window.open(projectInfo.github);
    },
    handleEditProject(projectInfo) {
      this.dialogFormVisible = true;
      this.form = {
        ...projectInfo,
        description: projectInfo.description.toString(),
      };
    },
    handleDeleteProject(projectInfo) {
      this.$confirm("此操作将永久删除该项目, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          await delProject(projectInfo.id);
          this.fetchData();
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    async handleConfirmDialog() {
      const obj = {
        ...this.form,
        description: this.form.description.split(","),
        order: Number(this.form.order),
      };
      await setProject(obj.id, obj);
      this.dialogFormVisible = false;
      this.fetchData();
      this.$message.success("修改成功");
    },
    handleUploadSuccess(url) {
      this.form.thumb = url;
    },
  },
};
</script>

<style scoped lang="scss">
</style>
